<template>
	<view>
		<y-advertisement v-if="setup==1" @close="close"></y-advertisement>
		<y-classification v-if="setup==2" @close="closeClassification" :list='classification'></y-classification>
		<view class="pb-10" v-if="setup==3">
			<index-1 :adsList="adsList" ref="index" @clear="getUser" :navList='navList' :notice="WEBCONFIG.notice"
				v-if="WEBCONFIG?.index_template=='1'" @open="openCustomer"></index-1>
			<index-2 :adsList="adsList" @clear="getUser" :navList='navList' ref="index" @sign="sign"
				@changeClass="getClass" :notice="WEBCONFIG.notice" v-if="WEBCONFIG?.index_template=='2'"></index-2>
		</view>
		<uv-popup ref='popupCustomer' round='20rpx'>
			<view class="rounded-12 bg-white py-20  " style="padding-left: 116rpx;padding-right: 116rpx;">
				<view class="text-center fs-16 fw-600">联系客服</view>
				<view class="py-20">
					<image class="w-180" mode="widthFix" :src="WEBCONFIG?.customer_qrcode" show-menu-by-longpress>
					</image>
				</view>
				<view class="text-center fs-17 ">长按保存图片</view>
			</view>
		</uv-popup>
		<y-tabbar v-if="setup==3"></y-tabbar>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import system from '@/utils/system'
	import { onLoad, onShow, onShareTimeline, onShareAppMessage } from '@dcloudio/uni-app'
	import { $http, $page, $tool, useStorage } from '@/utils/index'
	import { useUserStore, useRefs, useWebConfigStore } from "@/stores/index";
	import { $interstitialAd } from '@/utils/ad'
	const { WEBCONFIG } = useRefs(useWebConfigStore());
	const { USERINFO } = useRefs(useUserStore());
	const value = ref(0)
	const storage = useStorage()
	const popupCustomer = ref()
	const setup = ref(0)
	const adsList = ref([])
	const navList = ref([])
	const classification = ref([])
	const index = ref()
	onLoad(() => {
		getAds()
		getNav()
		uni.$once('initConfig', () => {
			// #ifdef MP||MP-TOUTIAO
			if (WEBCONFIG.value?.weixin_interstitial_status == '1') {
				$interstitialAd.init(WEBCONFIG.value?.weixin_interstitial_ad_id)
				$interstitialAd.show()
			}
			// #endif
			if (WEBCONFIG.value.screen_advertisement_status == 1) {
				close()
			} else {
				if (!useStorage().get('ADSHOW')) {
					setup.value = 1
				}
			}
		})
		if (setup.value == 0) {
			setup.value = 3
		}
		if (useUserStore().isLogin() == true) {
			getUser()
		}

		// #ifdef H5
		const script = document.createElement('script');
		script.type = "text/javascript";
		script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
		document.body.appendChild(script)
		script.onload = () => {
			mpShare()
		}
		// #endif
	})
	onShow(() => {
		if (index.value) {
			index.value.update()
		}
	})

	const getUser = () => {
		let id = useStorage().get('CATEGORY')
		$http.get('User/getUser', { classification_id: id }).then(res => {
			USERINFO.value = res.data
			useUserStore().setUserInfo(USERINFO.value)
		})
	}

	//轮播图
	const getAds = () => {
		$http.get('Ads/getList').then(res => {
			adsList.value = res.data
		})
	}
	//获取导航
	const getNav = () => {
		$http.get('Nav/getList').then(res => {
			navList.value = res.data
		})
	}
	//登录
	const login = () => {
		let appid = system.appid()
		let pid = storage.get('PID.' + appid) || 0
		uni.login({
			provider: 'weixin',
			success(res) {
				$http.post('User/login', { type: 'weixin', code: res.code, pid: pid }).then(res => {
					useUserStore().clearUserInfo()
					useUserStore().setUserInfo(res.data)
				})
			}
		})
	}

	//签到
	const sign = () => {
		if (!USERINFO.value.sign) {
			$http.get('User/sign').then(res => {
				$tool.toast('打卡成功')
				getUser()
			})
		}
	}

	//打开客服
	const openCustomer = () => {
		// #ifdef H5
		popupCustomer.value.open('center')
		return
		// #endif

		// #ifdef MP-TOUTIAO
		if (WEBCONFIG.value.douyin_im_status == '2') {
			popupCustomer.value.open('center')
		}
		// #endif
		// #ifdef MP-WEIXIN
		if (WEBCONFIG.value.customer_status == '1') {
			popupCustomer.value.open('center')
		} else {
			wx.openCustomerServiceChat({
				extInfo: {
					url: WEBCONFIG.value.customer_url
				},
				corpId: WEBCONFIG.value.customer_corpId, //必须和你小程序上的一致
				success(res) {
					console.log(res, 1)
				},
				fail(res) {
					console.log(res, 2)
				},
			})
		}
		// #endif

	}

	const close = () => {
		let id = storage.get('CATEGORY') || 0
		if (id == 0) {
			getClass()
		} else {
			setup.value = 3
			if (WEBCONFIG.value.custom_tabbar == '2') {
				uni.showTabBar()
			}
		}
	}
	const getClass = () => {
		$http.get('Category/getClassification').then(res => {
			classification.value = res.data
			if (classification.value.length) {
				setup.value = 2
			}
		})
	}

	const closeClassification = () => {
		setup.value = 3
		if (WEBCONFIG.value.custom_tabbar == '2') {
			uni.showTabBar()
		}
	}

	const mpShare = () => {
		$http.get('Mp/getConfig').then(res => {
			wx.ready(function() {
				wx.updateAppMessageShareData({
					title: WEBCONFIG.value.share_title, // 分享标题
					desc: WEBCONFIG.value.share_title, // 分享描述
					link: res.data.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imgUrl: WEBCONFIG.value.share_image, // 分享图标
					success: function() {},
					catch: (err) => {}
				})
			});
			wx.error(function(res) {});
			wx.config({
				debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
				...res.data
			});
		})
	}



	//分享
	onShareTimeline(() => {
		let id = USERINFO.value.id || 0
		return {
			title: WEBCONFIG.value?.share_title || WEBCONFIG.value?.web_name || '',
			query: 'pages/index/index?id=' + id,
			imageUrl: WEBCONFIG.value?.share_image || ''
		}
	})

	onShareAppMessage(() => {
		let id = USERINFO.value.id || 0
		return {
			title: WEBCONFIG.value?.share_title || WEBCONFIG.value?.web_name || '',
			path: 'pages/index/index?id=' + id,
			imageUrl: WEBCONFIG.value?.share_image || ''
		}
	})
</script>

<style scoped lang="scss">

</style>